package com.sf.fy.uav.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sf.fy.uav.common.enums.ServicePrefixEnum;
import com.sf.fy.uav.common.utils.RedisPrefixKey;
import com.sf.fy.uav.order.service.LoginOrderService;
import com.sf.fy.uav.task.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author LTQ
 */
@Service
public class LoginServiceOrderImpl implements LoginOrderService {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${wx.appid:wxa9995496636a5ee1}")
    private String appid;

    @Value("${wx.secret:ed609905dc5a817ea3d7091a5ea90654}")
    private String secret;


    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${proxy-url:sfproxy.intsit.sfcloud.local}")
    private String proxyUrl;


    @Value("${is-use-proxy:false}")
    private boolean isUseProxy;

    @Override
    public String getAccessToken() {
        String accessToken = (String) redisTemplate.opsForValue().get(ServicePrefixEnum.RESCUE + RedisPrefixKey.APPLET_ACCESS_TOKEN);
        if (ObjectUtils.isEmpty(accessToken)) {
            Map<String, String> params = new HashMap<>();
            params.put("grant_type", "client_credential");
            params.put("appid", appid);
            params.put("secret", secret);
            String body = HttpClientUtil.sendGetJsonRepeatProxy("https://api.weixin.qq.com/cgi-bin/token", proxyUrl, 80, params, isUseProxy);
            JSONObject object = JSON.parseObject(body);
            accessToken = object.getString("access_token");
            /*access_token有效时长*/
            int expires_in = object.getInteger("expires_in");
            /*过期时间减去10毫秒：10毫秒是网络连接的程序运行所占用的时间*/
            redisTemplate.opsForValue().set(ServicePrefixEnum.RESCUE + RedisPrefixKey.APPLET_ACCESS_TOKEN, accessToken);
            redisTemplate.expire(ServicePrefixEnum.RESCUE + RedisPrefixKey.APPLET_ACCESS_TOKEN, expires_in - 5, TimeUnit.SECONDS);

        }
        return accessToken;
    }

    @Override
    public String getExpressAccessToken() {
        String accessToken = (String) redisTemplate.opsForValue().get(ServicePrefixEnum.SAME_CITY + RedisPrefixKey.APPLET_ACCESS_TOKEN);
        if (ObjectUtils.isEmpty(accessToken)) {
            Map<String, String> params = new HashMap<>();
            params.put("grant_type", "client_credential");
            params.put("appid", appid);
            params.put("secret", secret);
            String body = HttpClientUtil.sendGetJsonRepeatProxy("https://api.weixin.qq.com/cgi-bin/token", proxyUrl, 80, params, isUseProxy);
            JSONObject object = JSON.parseObject(body);
            accessToken = object.getString("access_token");
            /*access_token有效时长*/
            int expires_in = object.getInteger("expires_in");
            /*过期时间减去10毫秒：10毫秒是网络连接的程序运行所占用的时间*/
            redisTemplate.opsForValue().set(ServicePrefixEnum.SAME_CITY + RedisPrefixKey.APPLET_ACCESS_TOKEN, accessToken);
            redisTemplate.expire(ServicePrefixEnum.SAME_CITY + RedisPrefixKey.APPLET_ACCESS_TOKEN, expires_in - 5, TimeUnit.SECONDS);

        }
        return accessToken;
    }


}
